﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

using Smg.XML;
using Smg.DAL;
using Smg.Model;
using Smg.Source;

namespace Smg
{
    public partial class frmMain : Form
    {
        StProject sp = new StProject();
        Thread[] threads;
        string[] strSqls;
        public delegate void delegateRun(string sql);

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.splitContainer1.Panel1Collapsed = true;
        }

        private void tsmiNewProject_Click(object sender, EventArgs e)
        {
            Form frm = new frmWiZard();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                this.LoadProject();
            }
        }

        private void tsmiOpenProject_Click(object sender, EventArgs e)
        {
            string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\StTools\\" + Application.ProductName;
            OpenFileDialog openFD = new OpenFileDialog();

            string strFileType = "项目文件(*.stproj)|*.stproj";

            openFD.Filter = strFileType;
            openFD.FilterIndex = 1;
            openFD.RestoreDirectory = true;
            openFD.InitialDirectory = filepath;
            if (openFD.ShowDialog() == DialogResult.OK)
            {

                filepath = openFD.FileName;
                Config.ProjectPath = filepath;
                this.LoadProject();
            }
            else
            {
                MessageBox.Show("打开失败，没有选择文件！", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void LoadProject()
        {
            try
            {
                sp = ProjectOp.GetProjectInfo(Config.ProjectPath);

                if (sp != null)
                {
                    ReadProject();
                    this.tsslProject.Text = "当前项目：" + Project.DBType;
                }
                else
                {
                    MessageBox.Show(Config.ProjectPath + "不是有效的项目文件！", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ReadProject()
        {
            if (sp != null)
            {
                 Project.ProjectName = sp.ProjectName;
                 Project.Url = sp.Url;
                 Project.FileType = sp.FileType;
                 Project.FilePath = sp.FilePath;
                 Project.GenerateMode = sp.GenerateMode;
                 Project.DBType = Project.GetConnection(sp.ConnectionString, "Get");

                 Secrecy se = new Secrecy();
                 Project.ConnectionString = se.decode(Project.GetConnection(sp.ConnectionString, null));

                 xmlOperatror xmlOp = new xmlOperatror(Config.ProjectPath);
                 Project.SqlTemplates = xmlOp.GetXmlAsDs("//Project/SqlTemplates");

                if(StaticUtil.IsDsHasData(Project.SqlTemplates))
                {
                    this.dataGridView1.DataSource=Project.SqlTemplates.Tables[0].DefaultView;
                    this.dataGridView1.Refresh();
                }
            }
        }

        private void tsmiExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void tsmiOption_Click(object sender, EventArgs e)
        {

        }

        private void tsmiUp_Click(object sender, EventArgs e)
        {

        }

        private void tsmiAbout_Click(object sender, EventArgs e)
        {

        }

        private void tsbExeCute_Click(object sender, EventArgs e)
        {
            try
            {
                Urlset us = this.CreateUrlset();

                string patch = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\StTools\\" + Application.ProductName + "\\Sitemap.xml";

                ObjectXMLSerializer<Urlset>.Save(us, patch,XMLFormat.Sitemap);

                MessageBox.Show("OK." + patch);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private Urlset CreateUrlset()
        {
            Urlset us = new Urlset();

            UrlTemplate ut = new UrlTemplate();
            ut.loc = "http://www.sooot.cn/";
            ut.lastmod = "2008-09-29";
            ut.changefreq = "weekly";
            ut.priority = "0.5";
            us.url.Add(ut);

            ut.loc = "http://www.sooot.cn/";
            ut.lastmod = "2008-09-30";
            ut.changefreq = "weekly";
            ut.priority = "0.8";
            us.url.Add(ut);


            return us;
        }

        private void tsbStop_Click(object sender, EventArgs e)
        {

        }

        private void tsbAdd_Click(object sender, EventArgs e)
        {

        }

        private void tsbOpen_Click(object sender, EventArgs e)
        {

        }

        private void tsbSave_Click(object sender, EventArgs e)
        {

        }

        private void tsbSetting_Click(object sender, EventArgs e)
        {

        }

        private void tsbHome_Click(object sender, EventArgs e)
        {

        }

        private void GetSiteMap()
        {
            this.dsSqlTemplate = Project.SqlTemplates;
            if (StaticUtil.IsDsHasData(dsSqlTemplate))
            {
                for (int i = 0; i < this.dsSqlTemplate.Tables[0].Rows.Count; i++)
                {
                    strSqls[i] = dsSqlTemplate.Tables[0].Rows[i]["Sql"].ToString();
                    ThreadStart ts = new ThreadStart(this.run);
                    threads[i] = new Thread(ts);
                    threads[i].Start();
                }
            }
        }

        private static DataSet _dsSqlTemplate;
        private DataSet dsSqlTemplate
        {
            get
            {
                return _dsSqlTemplate;
            }
            set
            {
                _dsSqlTemplate = value;
            }
        }

        private void run()
        {
            delegateRun dr = new delegateRun(AddInfoToDataGridView("ss"));
            this.Invoke(dr);
        }

        private void AddInfoToDataGridView(string sql)
        {
            if (Project.DBType.ToLower().Equals("access"))
            {
                OleDBOpration dbo = new OleDBOpration(Project.ConnectionString);
                DataSet ds = dbo.GetResult(sql);
                if (StaticUtil.IsDsHasData(ds))
                {
 
                }
            }
            else if(Project.DBType.ToLower().Equals("mssql"))
            {
                MsSqlDBOpration msdbo = new MsSqlDBOpration(Project.ConnectionString);
            }
        }
    }
}